package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description NursingPlanImpl
 * @Author Wen Jie
 * @Date 2024-09-06
 */
@Service
public class NursingPlanImpl implements NursingPlanService{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlan> nursingPlans = nursingPlanMapper.getAll();
        List<NursingPlanVo> nursingPlanVos = BeanUtil.copyToList(nursingPlans, NursingPlanVo.class);
        return nursingPlanVos;
    }
    @Override
    public void addNursingPlan(NursingPlanVo nursingPlan) {
        nursingPlan.setStatus(1);
        nursingPlanMapper.addNursingPlan(nursingPlan);
        nursingPlan.getProjectPlans().forEach(item -> {
            item.setPlanId(nursingPlan.getId());
        });
        nursingPlanMapper.addNursingPlanProject(nursingPlan.getProjectPlans());
    }

    @Override
    public PageResponse<NursingPlanVo> getPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> nursingPlans = nursingPlanMapper.getPage(name,status);
        return PageResponse.of(nursingPlans, NursingPlanVo.class);
    }

    @Override
    public NursingPlanVo getById(Long id) {
        NursingPlan nursingPlan = nursingPlanMapper.getById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        return nursingPlanVo;
    }

    @Override
    public void updateNursingPlan(NursingPlanVo nursingPlanVo) {
        Long id = nursingPlanVo.getId();
        int i = countById(id);
        if(i>0){
            throw new RuntimeException("该计划已被使用");
        }
        nursingPlanMapper.updateNursingPlan(nursingPlanVo);

        nursingPlanVo.getProjectPlans().forEach(item -> {
            item.setPlanId(nursingPlanVo.getId());
        });
        //清除该计划下的所有项目
        nursingPlanVo.getProjectPlans().forEach(item -> {
            nursingPlanMapper.deletePlanProjectById(item.getPlanId());
        });
        //将项目新的项目重新添加
        nursingPlanMapper.addNursingPlanProject(nursingPlanVo.getProjectPlans());
    }

    @Override
    public void deleteNursingPlan(Long id) {
        int i = countById(id);
        if(i>0){
            throw new RuntimeException("该计划已被使用");
        }
        int status = getStatusById(id);
        if(status == 1){
            throw new RuntimeException("该计划已启用，无法删除");
        }
        nursingPlanMapper.deletePlanProjectById(id);
        nursingPlanMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        int i = countById(id);
        if(i>0){
            throw new RuntimeException("该计划已被使用");
        }
        nursingPlanMapper.updateStatusById(id,status);
    }

    public int countById(Long id) {
        int count = nursingPlanMapper.countById(id);
        return count;
    }
    public int getStatusById(Long id) {
        int status = nursingPlanMapper.getStatusById(id);
        return status;
    }
}
